﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LTGmaster
{
	public class Game
	{
		public static bool IsEnemyTurn(int turnsDone, bool meFirst)
		{
			bool firstPlayerTurn = (turnsDone & 1) == 0;
			return meFirst == firstPlayerTurn;
		}

		public Game(bool meFirst)
		{
			this.meFirst = meFirst;
		}

		readonly bool meFirst;
		readonly LinkedList<Turn> turns = new LinkedList<Turn>();
		readonly SlotState[] mySlots = Enumerable.Range(0, 256)
			.Select(any => new SlotState()).ToArray();
		readonly SlotState[] enemy = Enumerable.Range(0, 256)
			.Select(any => new SlotState()).ToArray();
		/// <summary>
		/// Enumerates all turns since game start. By default starts from the last turn.
		/// </summary>
		/// <param name="reverse">true to enumerate from the last turn,
		/// false - to enumerate from the first</param>
		public IEnumerable<Turn> History(bool reverse = true)
		{
			if (!reverse) foreach (var node in turns) yield return node;
			else {
				var node = turns.Last;
				while (node != null) {
					yield return node.Value;
					node = node.Previous;
				}
			}
		}

		public void Turn(Turn turn)
		{
			bool isEnemyTurn = IsEnemyTurn(turns.Count, meFirst);
			SlotState[] prop = isEnemyTurn ? enemy : mySlots;
			SlotState[] opp = isEnemyTurn ? mySlots : enemy;
			// TODO: simulate game
		}

		public SlotState[] MySlots
		{
			get
			{
				return mySlots;
			}
		}
		public SlotState[] Enemy
		{
			get
			{
				return enemy;
			}
		}
	}
}
